package com.yc.other.mail;

import java.util.Properties;

import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

/**
 * TODO
 * @author yuancan
 * @date  2018-02-05 20:07 
 * @since 3.0.0.3
 */
public class Sendmail {
	private static final String FROM1 = "yunduanyou@126.com";
	private static final String TO1 = "yuancan@360.com";
	private static final String PWD1 = "mc10000d2017";
	
	private static final String FROM2 = "yunduanyou@126.com";
	private static final String TO2 = "yuancan@360.com";
	private static final String PWD3 = "mc10000d2017";
	
//	public static void main(String[] args) throws Exception {
//		Properties prop = new Properties();
//		prop.setProperty("mail.host", "smtp.126.com");
//		prop.setProperty("mail.transport.protocol", "smtp");
//		prop.setProperty("mail.smtp.port", "25");
//		prop.setProperty("mail.smtp.auth", "true");
//		
//		//1. 创建session
//		Session session = Session.getInstance(prop);
//		//开启session的debug模式，可以看到程序发送Email的运行状态
//		session.setDebug(true);
//		//2. 通过session得到transport对象
//		Transport ts = session.getTransport();
//		//3. 输入用户名密码
//		ts.connect("smtp.126.com", "yunduanyou", PWD1);
//		//4. 创建邮件
//		MimeMessage message = createSimpleMail(session);
//		//5. 发送邮件
//		ts.sendMessage(message, message.getAllRecipients());
//		ts.close();
//	}
	
	public void send126() throws Exception {
		Properties prop = new Properties();
		prop.setProperty("mail.smtp.host", "smtp.126.com");
		prop.setProperty("mail.transport.protocol", "smtp");
		prop.setProperty("mail.smtp.port", "25");
		prop.setProperty("mail.smtp.auth", "true");
		
		//1. 创建session
		Session session = Session.getInstance(prop);
		//开启session的debug模式，可以看到程序发送Email的运行状态
		session.setDebug(true);
		//2. 通过session得到transport对象
		Transport ts = session.getTransport();
		//3. 输入用户名密码
		ts.connect("smtp.126.com", "yunduanyou", PWD1);
		//4. 创建邮件
		MimeMessage message = createSimpleMail(session);
		//5. 发送邮件
		ts.sendMessage(message, message.getAllRecipients());
		ts.close();
	}
	
	public void sendExchange() {
		
	}

	public MimeMessage createSimpleMail(Session session) 
			throws Exception {
		//创建邮件对象
		MimeMessage message = new MimeMessage(session);
		//发件人
		message.setFrom(new InternetAddress(FROM1));
		//收件人
		message.setRecipient(Message.RecipientType.TO, new InternetAddress(TO1));
		//标题
		message.setSubject("只包含文本的简单邮件");
		//邮件内容
		message.setContent("你好啊！", "test/html;charset=UTF-8");
		//返回创建好的邮件对象
		return message;
		
	}
	
	public static void main(String[] args) throws Exception {
		Sendmail sd = new Sendmail();
		sd.send126();
	}
}
